#!/bin/bash

. `dirname $0`/include.sh
_include "detect.sh"

vendor=`linux_vendor`
distro=`linux_distro`

function _setup_install()
{
    _PACKAGE="$1"

    rpm -q $_PACKAGE 1>/dev/null 2>&1
    [ $? -eq 0 ] && echo "Package $_PACKAGE already installed!" && return

    echo "Install $_PACKAGE by setup tool ......"
    [ -f /sbin/yast2 ] && /sbin/yast2 --install $_PACKAGE
    [ -f /usr/bin/yum ] && /usr/bin/yum -y install $_PACKAGE
}

function _rpm_install()
{
    _RPM_PATH="$1"
    _RPM_NAME=`echo $_RPM_PATH | awk -F'/' '{print $NF}' | awk -F'.' '{print $1}' | awk -F '-' '{print $1}'`

    rpm -q $_RPM_NAME 1>/dev/null 2>&1
    [ $? -eq 0 ] && echo "Package $_RPM_NAME already installed!" && return

    echo "Install $_RPM_PATH by rpm ......"
    [ -f $_RPM_PATH ] && rpm -Uvh $_RPM_PATH
}

function _python_install()
{
    _PYTHON_DIR="$1"
    echo "Install $_PYTHON_DIR by python ......"
    if [ -d $_PYTHON_DIR -a -f $_PYTHON_DIR/setup.py  ]
    then
        _CDIR=`pwd`
        cd $_PYTHON_DIR
        python ./setup.py build 1>/dev/null 2>&1
        python ./setup.py install 1>/dev/null 2>&1
        cd $_CDIR
    fi
}

ANY="any_os"
function _install()
{
    _NAME="$1"
    _ONEX="$2" # Just a "on/except"
    _COND="$3"

    [ "$_COND" != "$vendor" -a "$_COND" != "$distro" -a "$_COND" != "$ANY" ] && echo "Skipped $_NAME installation since it requires \"$_COND\"" && return

    [ -d "$_NAME" ] && _python_install "$_NAME" && return
    [ -f "$_NAME" ] && _rpm_install "$_NAME" && return
    [ ! -d "$_NAME" -a ! -f "$_NAME" ] && _setup_install "$_NAME"
}

# for ezbmc ezpci hpmgmtbase
_install gcc			on $ANY
_install gcc-c++		on $ANY
_install libstdc++		on redhat
_install libstdc++-devel	on $ANY
_install gdb			on $ANY
_install libxml2-devel		on $ANY
_install kernel-headers		on redhat
_install linux-kernel-headers	on sles11
_install kernel-devel		on redhat
_install kernel-source		on suse
_install dbus-devel		on redhat
_install dbus-1-devel		on suse
_install rpm-build		on redhat
_install rpm-devel		on $ANY

# For hprasd
[ -d /etc/init.d/salinfo_decode ] && /etc/init.d/salinfo_decode stop 2>/dev/null
_install python-devel		on sles11
_install python-gobject2	on sles11
_install python-gobject2-devel	on sles11
_install dbus-1-python		on sles11
_install dbus-1-python-devel	on sles11

# For hp-smx
_install autoconf		on $ANY
_install automake		on $ANY

_install libcurl-devel		on sles11
_install python-pywbem		on sles11
_install python-setuptools	on sles11

_python_install smx_require/pywbem	on $ANY
_rpm_install    smx_require/scons.rpm	on $ANY


# tog-pegasus for distro other than sles11
[ $distro == "sles11" ] && exit 0
arch=`cpu_arch_ifil`
PGS_PATH="http://linux.fc.hp.com/manageability/downloads/smx/smxtp/"
PGS_VER="2.7.1-17hp"
[ "$arch" == "ia64" -a "$distro" == "sles10" ] && PGS_VER="2.7.1-18hp"
PGS_RPM="tog-pegasus-$PGS_VER.$distro.$arch.rpm"
PGS_DEV_RPM="tog-pegasus-devel-$PGS_VER.$distro.$arch.rpm"
wget $PGS_PATH$PGS_RPM && mv $PGS_RPM smx_require/
wget $PGS_PATH$PGS_DEV_RPM && mv $PGS_DEV_RPM smx_require/
_install     smx_require/$PGS_RPM	on $ANY
_install     smx_require/$PGS_DEV_RPM	on $ANY



